<!DOCTYPE html><html lang="zh-Hans"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"><meta name="description" content="PHP学习笔记"><meta name="keywords" content="ctf,note,tips"><meta name="author" content="MOZac Connecter"><meta name="copyright" content="MOZac Connecter"><title>PHP学习笔记 | MOZac的小屋</title><link rel="shortcut icon" href="/melody-favicon.ico"><link rel="stylesheet" href="/css/index.css?version=1.9.0"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/font-awesome@latest/css/font-awesome.min.css?version=1.9.0"><meta name="format-detection" content="telephone=no"><meta http-equiv="x-dns-prefetch-control" content="on"><link rel="dns-prefetch" href="https://cdn.jsdelivr.net"><script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><script>(adsbygoogle = window.adsbygoogle || []).push({
  google_ad_client: 'ca-pub-7313518215964899',
  enable_page_level_ads: 'true'
});
</script><meta name="google-site-verification" content="UA-186375523"><meta http-equiv="Cache-Control" content="no-transform"><meta http-equiv="Cache-Control" content="no-siteapp"><script>var GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: undefined,
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  hexoVersion: '5.3.0'
} </script><meta name="generator" content="Hexo 5.3.0"><link rel="alternate" href="/atom.xml" title="MOZac的小屋" type="application/atom+xml">
</head><body><canvas class="fireworks"></canvas><i class="fa fa-arrow-right" id="toggle-sidebar" aria-hidden="true"></i><div id="sidebar" data-display="true"><div class="toggle-sidebar-info text-center"><span data-toggle="切换文章详情">切换站点概览</span><hr></div><div class="sidebar-toc"><div class="sidebar-toc__title">目录</div><div class="sidebar-toc__progress"><span class="progress-notice">你已经读了</span><span class="progress-num">0</span><span class="progress-percentage">%</span><div class="sidebar-toc__progress-bar"></div></div><div class="sidebar-toc__content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#ST-2020-12-3-No-5"><span class="toc-number">1.</span> <span class="toc-text">ST.2020&#x2F;12&#x2F;3 No.5</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#PHP%E7%9B%B8%E5%85%B3%E8%AF%AD%E6%B3%95%EF%BC%9A"><span class="toc-number">2.</span> <span class="toc-text">PHP相关语法：</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#PHP%E5%8F%98%E9%87%8F%E4%BD%9C%E7%94%A8%E5%9F%9F%EF%BC%9A"><span class="toc-number">3.</span> <span class="toc-text">PHP变量作用域：</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1%E3%80%81local%EF%BC%88%E6%9C%AC%E5%9C%B0%E7%9A%84%EF%BC%89%E2%80%94%E2%80%94-%E5%B1%80%E9%83%A8%E5%8F%98%E9%87%8F%E7%9A%84%E4%BD%9C%E7%94%A8%E5%9F%9F"><span class="toc-number">3.1.</span> <span class="toc-text">1、local（本地的）—— 局部变量的作用域</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2%E3%80%81global%EF%BC%88%E6%80%BB%E4%BD%93%E7%9A%84%EF%BC%89%E2%80%94%E2%80%94-%E5%85%A8%E5%B1%80%E5%8F%98%E9%87%8F%E7%9A%84%E4%BD%9C%E7%94%A8%E5%9F%9F"><span class="toc-number">3.2.</span> <span class="toc-text">2、global（总体的）—— 全局变量的作用域</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3%E3%80%81static%EF%BC%88%E9%9D%99%E6%80%81%E7%9A%84%EF%BC%89%E2%80%94%E2%80%94-static-%E4%BD%9C%E7%94%A8%E5%9F%9F"><span class="toc-number">3.3.</span> <span class="toc-text">3、static（静态的）—— static 作用域</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4%E3%80%81parameter%EF%BC%88%E5%8F%82%E6%95%B0%EF%BC%89%E2%80%94%E2%80%94%E5%8F%82%E6%95%B0%E4%BD%9C%E7%94%A8%E5%9F%9F"><span class="toc-number">3.4.</span> <span class="toc-text">4、parameter（参数）——参数作用域</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#PHP%E7%9A%84%E8%BE%93%E5%87%BA%EF%BC%9A"><span class="toc-number">4.</span> <span class="toc-text">PHP的输出：</span></a></li></ol></div></div><div class="author-info hide"><div class="author-info__avatar text-center"><img src="https://s3.ax1x.com/2020/12/21/r0TN5t.png"></div><div class="author-info__name text-center">MOZac Connecter</div><div class="author-info__description text-center">安全人Mozac的平凡日常</div><div class="follow-button"><a target="_blank" rel="noopener" href="https://space.bilibili.com/13299663">关注我</a></div><hr><div class="author-info-articles"><a class="author-info-articles__archives article-meta" href="/archives"><span class="pull-left">文章</span><span class="pull-right">13</span></a><a class="author-info-articles__tags article-meta" href="/tags"><span class="pull-left">标签</span><span class="pull-right">22</span></a><a class="author-info-articles__categories article-meta" href="/categories"><span class="pull-left">分类</span><span class="pull-right">4</span></a></div><hr><div class="author-info-links"><div class="author-info-links__title text-center">朋友们</div><a class="author-info-links__name text-center" target="_blank" rel="noopener" href="https://www.vincehut.top/">Vince迷航者</a></div></div></div><div id="content-outer"><div class="no-bg" id="top-container"><div id="page-header"><span class="pull-left"> <a id="site-name" href="/">MOZac的小屋</a></span><i class="fa fa-bars toggle-menu pull-right" aria-hidden="true"></i><span class="pull-right menus">   <a class="site-page" href="/">主页</a><a class="site-page" href="/archives">文章</a><a class="site-page" href="/tags">标签</a><a class="site-page" href="/categories">分类</a></span><span class="pull-right"></span></div><div id="post-info"><div id="post-title">PHP学习笔记</div><div id="post-meta"><time class="post-meta__date"><i class="fa fa-calendar" aria-hidden="true"></i> 2020-12-28</time><span class="post-meta__separator">|</span><i class="fa fa-inbox post-meta__icon" aria-hidden="true"></i><a class="post-meta__categories" href="/categories/%E7%AC%94%E8%AE%B0/">笔记</a></div></div></div><div class="layout" id="content-inner"><article id="post"><div class="article-container" id="post-content"><h2 id="ST-2020-12-3-No-5"><a href="#ST-2020-12-3-No-5" class="headerlink" title="ST.2020/12/3 No.5"></a>ST.2020/12/3 No.5</h2><h2 id="PHP相关语法："><a href="#PHP相关语法：" class="headerlink" title="PHP相关语法："></a>PHP相关语法：</h2><figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?php</span> <span class="comment">//&lt;-标识符</span></span><br><span class="line"><span class="variable">$x</span> = <span class="string">&quot;xxx&quot;</span>;</span><br><span class="line"><span class="variable">$y</span> = <span class="number">10</span>;</span><br><span class="line"><span class="variable">$z</span> = <span class="number">10.5</span>;</span><br><span class="line"><span class="comment">//PHP是弱类型语言，因此不用声明变量名</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">test</span>(<span class="params"><span class="comment">/*可传参*/</span></span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">  xxx；<span class="comment">//定义函数</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">echo</span> <span class="string">&quot;&lt;/br&gt;&quot;</span>;</span><br><span class="line"><span class="keyword">echo</span> PHP_EOL;<span class="comment">//可使用HTML和CSS</span></span><br><span class="line"><span class="comment">//单行注释格式</span></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">进行</span></span><br><span class="line"><span class="comment">多行</span></span><br><span class="line"><span class="comment">注释</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="meta">?&gt;</span>  <span class="comment">//&lt;-结尾标识符</span></span><br></pre></td></tr></table></figure>
<h2 id="PHP变量作用域："><a href="#PHP变量作用域：" class="headerlink" title="PHP变量作用域："></a>PHP变量作用域：</h2><p>变量的作用域是脚本中变量可被引用和使用的部分，PHP 中有四种不同的变量作用域:</p>
<table>
<thead>
<tr>
<th align="left">序号</th>
<th align="left">作用域</th>
<th align="left">描述</th>
</tr>
</thead>
<tbody><tr>
<td align="left">1</td>
<td align="left">local</td>
<td align="left">局部作用域</td>
</tr>
<tr>
<td align="left">2</td>
<td align="left">global</td>
<td align="left">全局作用域</td>
</tr>
<tr>
<td align="left">3</td>
<td align="left">static</td>
<td align="left">静态作用域</td>
</tr>
<tr>
<td align="left">4</td>
<td align="left">parameter</td>
<td align="left">函数参数作用域</td>
</tr>
</tbody></table>
<h3 id="1、local（本地的）——-局部变量的作用域"><a href="#1、local（本地的）——-局部变量的作用域" class="headerlink" title="1、local（本地的）—— 局部变量的作用域"></a><strong>1、local（本地的）—— 局部变量的作用域</strong></h3><p>在 PHP 函数内部声明的变量是局部变量，仅能在函数内部访问。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">&lt;?php</span><br><span class="line">function test()</span><br><span class="line">&#123;</span><br><span class="line">    $a &#x3D; 15;</span><br><span class="line">    echo &quot;内部输出结果：&quot; . $a;</span><br><span class="line">&#125;</span><br><span class="line">echo &quot;外部输出结果：&quot; . $a;  &#x2F;&#x2F; 无法访问变量 a</span><br><span class="line">echo PHP_EOL;</span><br><span class="line">test();</span><br><span class="line">?&gt;</span><br></pre></td></tr></table></figure>
<p>输出结果：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">外部输出结果：</span><br><span class="line">内部输出结果：15</span><br></pre></td></tr></table></figure>
<h3 id="2、global（总体的）——-全局变量的作用域"><a href="#2、global（总体的）——-全局变量的作用域" class="headerlink" title="2、global（总体的）—— 全局变量的作用域"></a><strong>2、global（总体的）—— 全局变量的作用域</strong></h3><p>在所有函数外部定义的变量是全局变量，除了函数外，全局变量可以被脚本中的任何部分访问、要在一个函数中访问一个全局变量，需要使用 global 关键字。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">&lt;?php</span><br><span class="line">$x &#x3D; 5;</span><br><span class="line">$y &#x3D; 10;</span><br><span class="line">$z &#x3D; 0;</span><br><span class="line">function test()</span><br><span class="line">&#123;</span><br><span class="line">    global$x,$y,$z;</span><br><span class="line">    $z &#x3D; $x + $y;</span><br><span class="line">&#125;</span><br><span class="line">test();</span><br><span class="line">echo $z;</span><br><span class="line">?&gt;</span><br></pre></td></tr></table></figure>
<p>结果：<strong>15</strong><br>PHP 将所有的全局变量存储在一个名为**$GLOBALS[index]<strong>的数组里，</strong>index**保存变量的名称。</p>
<p>这个数组可以在函数内部访问，也可以直接用来更新全局变量。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">&lt;?php</span><br><span class="line">$x &#x3D; 5;</span><br><span class="line">$y &#x3D; 10;</span><br><span class="line">$z &#x3D; 0;</span><br><span class="line">function test()</span><br><span class="line">&#123;</span><br><span class="line">    $GLOBALS[&#39;z&#39;] &#x3D; $GLOBALS[&#39;x&#39;]+$GLOBALS[&#39;y&#39;];</span><br><span class="line">&#125;</span><br><span class="line">Test();</span><br><span class="line">echo $z;</span><br><span class="line">?&gt;</span><br></pre></td></tr></table></figure>
<p>结果：<strong>15</strong></p>
<h3 id="3、static（静态的）——-static-作用域"><a href="#3、static（静态的）——-static-作用域" class="headerlink" title="3、static（静态的）—— static 作用域"></a><strong>3、static（静态的）—— static 作用域</strong></h3><p>当一个函数执行完成时，它的所有变量通常都会被删除。</p>
<p>然而，有时需要局部变量不要被删除，要做到这一点，请在您第一次声明变量时使用 static 关键字。</p>
<p>当一个函数完成时，它的所有变量通常都会被删除。然而，有时候您希望某个局部变量不要被删除。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">&lt;?php</span><br><span class="line">function test() &#123;</span><br><span class="line">    static $x&#x3D;0;</span><br><span class="line">    echo $x . &quot; &quot;;</span><br><span class="line">    $x++;</span><br><span class="line">&#125;</span><br><span class="line">test();</span><br><span class="line">test();</span><br><span class="line">test();</span><br><span class="line">test();</span><br><span class="line">?&gt;</span><br></pre></td></tr></table></figure>
<p>结果：<strong>0 1 2 3</strong><br>每次调用函数时， 该变量将会保留请前函的前被调用的值一次。</p>
<h3 id="4、parameter（参数）——参数作用域"><a href="#4、parameter（参数）——参数作用域" class="headerlink" title="4、parameter（参数）——参数作用域"></a><strong>4、parameter（参数）——参数作用域</strong></h3><p>参数是通过调用代码将值传递给函数的局部变量。</p>
<p>关于局部变量和全局变量的优先级，因为在PHP中函数都有自己单独的作用域，所以在局部变量的优先级要大于全局变量（在你不声明这个变量之前），声明之后此变量就变成了局部变量如果修改值那么全局变量的值也会改变。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line">&lt;?php</span><br><span class="line">$a &#x3D; 10;</span><br><span class="line">$b &#x3D; 5;</span><br><span class="line">function test()</span><br><span class="line">&#123;</span><br><span class="line">    $a &#x3D; 15;</span><br><span class="line">    $b &#x3D; 5;</span><br><span class="line">    $z &#x3D; $a-$b;</span><br><span class="line">    echo $z;</span><br><span class="line">&#125;</span><br><span class="line">test();</span><br><span class="line">function test1()</span><br><span class="line">&#123;</span><br><span class="line">         global $a,$b;</span><br><span class="line">         $a &#x3D; 15;</span><br><span class="line">         $b &#x3D; 5;</span><br><span class="line">         $z &#x3D; $a-$b;</span><br><span class="line">         echo PHP_EOL;</span><br><span class="line">         echo $z;</span><br><span class="line">&#125;</span><br><span class="line">test1();</span><br><span class="line">function test2()</span><br><span class="line">&#123;</span><br><span class="line">    global $a,$b;</span><br><span class="line">    $z&#x3D; $a-$b;</span><br><span class="line">    echo PHP_EOL;</span><br><span class="line">    echo $z;</span><br><span class="line">&#125;</span><br><span class="line">test2();</span><br><span class="line">?&gt;</span><br></pre></td></tr></table></figure>
<p>结果：<strong>10 10 10</strong><br>PS.在不含GLOBAL等关键字的情况下，局部作用域内无法访问全局变量，全局作用域下也无法访问局部变量</p>
<h2 id="PHP的输出："><a href="#PHP的输出：" class="headerlink" title="PHP的输出："></a>PHP的输出：</h2></div></article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">MOZac Connecter</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://mozac-void.yixiangtang.icu/2020/12/28/phpnote/">https://mozac-void.yixiangtang.icu/2020/12/28/phpnote/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://mozac-void.yixiangtang.icu">MOZac的小屋</a>！</span></div></div><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/ctf/">ctf</a><a class="post-meta__tags" href="/tags/note/">note</a><a class="post-meta__tags" href="/tags/tips/">tips</a></div><div class="social-share pull-right" data-disabled="facebook"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js@1.0.16/dist/css/share.min.css"><script src="https://cdn.jsdelivr.net/npm/social-share.js@1.0.16/dist/js/social-share.min.js"></script><nav id="pagination"><div class="prev-post pull-left"><a href="/2020/12/31/newyear/"><i class="fa fa-chevron-left">  </i><span>再见2020，你好2021</span></a></div><div class="next-post pull-right"><a href="/2020/12/28/TSGWCTF/"><span>2020天山固网杯线上赛WP</span><i class="fa fa-chevron-right"></i></a></div></nav><div class="post-adv"><iframe frameborder="no" border="0" marginwidth="0" marginheight="0" width=728 height=110 src="//music.163.com/outchain/player?type=0&id=2110349418&auto=1&height=90"></iframe></div><div id="lv-container" data-id="city" data-uid="MTAyMC81MjI0My8yODcyMg=="><script>(function(d, s) {
    var j, e = d.getElementsByTagName(s)[0];
    if (typeof LivereTower === 'function') { return; }
    j = d.createElement(s);
    j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
    j.async = true;
    e.parentNode.insertBefore(j, e);
})(document, 'script');</script></div></div></div><footer><div class="layout" id="footer"><div class="copyright">&copy;2019 - 2021 By MOZac Connecter</div><div class="framework-info"><span>驱动 - </span><a target="_blank" rel="noopener" href="http://hexo.io"><span>Hexo</span></a><span class="footer-separator">|</span><span>主题 - </span><a target="_blank" rel="noopener" href="https://github.com/Molunerfinn/hexo-theme-melody"><span>Melody</span></a></div><div class="icp"><a><span>鲁ICP备2020049110号</span></a></div><div class="busuanzi"><script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><span id="busuanzi_container_page_pv"><i class="fa fa-file"></i><span id="busuanzi_value_page_pv"></span><span></span></span></div></div></footer><i class="fa fa-arrow-up" id="go-up" aria-hidden="true"></i><script src="https://cdn.jsdelivr.net/npm/animejs@latest/anime.min.js"></script><script src="https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js"></script><script src="https://cdn.jsdelivr.net/npm/velocity-animate@latest/velocity.min.js"></script><script src="https://cdn.jsdelivr.net/npm/velocity-ui-pack@latest/velocity.ui.min.js"></script><script src="/js/utils.js?version=1.9.0"></script><script src="/js/fancybox.js?version=1.9.0"></script><script src="/js/sidebar.js?version=1.9.0"></script><script src="/js/copy.js?version=1.9.0"></script><script src="/js/fireworks.js?version=1.9.0"></script><script src="/js/transition.js?version=1.9.0"></script><script src="/js/scroll.js?version=1.9.0"></script><script src="/js/head.js?version=1.9.0"></script><script id="ribbon" src="/js/third-party/canvas-ribbon.js" size="150" alpha="0.6" zIndex="-1" data-click="false"></script><script>if(/Android|webOS|iPhone|iPod|iPad|BlackBerry/i.test(navigator.userAgent)) {
  $('#nav').addClass('is-mobile')
  $('footer').addClass('is-mobile')
  $('#top-container').addClass('is-mobile')
}</script></body></html>